/**
 * 
 */
package com.mobile2you.nci.android.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile2you.nci.android.MenuCentreMapDetail;
import com.mobile2you.nci.android.R;
import com.mobile2you.nci.android.domain.Centres;

/**
 * @author JBromo
 * 
 */
public class ListCentreAdapter extends BaseAdapter {

	List<Centres> listcentres = new ArrayList<Centres>();

	boolean express;

	class CentreItemView extends LinearLayout {
		TextView name;

		public CentreItemView(Context context, Centres centres) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.centre_item_view,
					this, true);
			name = (TextView) findViewById(R.id.label_centre_item_name);
			name.setText(centres.getTitle());
		}

		private void reuse(Centres centres) {
			name.setText(centres.getTitle());
		}
	}

	public class DistanceComparator implements Comparator<Centres> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(Centres lhs, Centres rhs) {
			if (lhs.getDistance() == rhs.getDistance()) {
				return 0;
			}
			return lhs.getDistance() > rhs.getDistance() ? 1 : -1;

		}

	}

	class ExpressItemView extends LinearLayout {
		TextView name;
		TextView distance;
		ImageView callView, directionView;

		public ExpressItemView(Context context, final Centres centres) {
			super(context);
			LayoutInflater.from(context).inflate(
					R.layout.centre_express_item_view, this, true);
			name = (TextView) findViewById(R.id.label_centre_item_name);
			name.setText(centres.getTitle());

			this.setTag(centres.getId());
			distance = (TextView) findViewById(R.id.label_distance);
			distance.setText(Utils.getDistanceAsString(centres.getDistance()));
			callView = (ImageView) findViewById(R.id.icon_express_call);
			directionView = (ImageView) findViewById(R.id.icon_express_location);
			callView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Utils.lanceCall(getContext(), centres.getPhone());
				}
			});
			directionView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Utils.centreSelected = centres;
					System.out.println("Centre seleted :" + centres);
					Utils.lancerActiviter(getContext(),
							MenuCentreMapDetail.class);

				}
			});
		}

		private void reuse(final Centres centres) {
			distance.setText(Utils.getDistanceAsString(centres.getDistance()));
			name.setText(centres.getTitle());
			callView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Utils.lanceCall(getContext(), centres.getPhone());
				}
			});
			directionView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Utils.centreSelected = centres;
					System.out.println("Centre seleted :" + centres);
					Utils.lancerActiviter(getContext(),
							MenuCentreMapDetail.class);

				}
			});
		}

	}

	/**
	 * @param listNews
	 */
	public ListCentreAdapter(List<Centres> listcentres, boolean express) {
		this.listcentres = listcentres;
		this.express = express;
		if (express) {
			Collections.sort(this.listcentres, new DistanceComparator());
		} else {
			Collections.sort(this.listcentres);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listcentres.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listcentres.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Centres centres = listcentres.get(position);
		if (express) {
			ExpressItemView centreItemView = null;
			if (convertView != null
					&& convertView.getTag().toString()
							.equalsIgnoreCase(String.valueOf(centres.getId()))) {
				centreItemView = (ExpressItemView) convertView;
				centreItemView.reuse(centres);
				return centreItemView;
			}
			return new ExpressItemView(parent.getContext(), centres);
		} else {
			CentreItemView centreItemView = null;
			if (convertView != null) {
				centreItemView = (CentreItemView) convertView;
				centreItemView.reuse(centres);
				return centreItemView;
			}
			return new CentreItemView(parent.getContext(), centres);
		}

	}
}
